package edu.jptest.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.jptest.bean.ExamineQuestionType;
import edu.jptest.util.GetDataBaseConnection;

public class ExamineQuestionTypeDAO {
	// 插入卷题型
	public boolean insertExamineQuestionType(
			ExamineQuestionType examineQuestionType) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "insert into e_q_type(examination_paper_id,question_type_id,type_score,question_type_sum) values(?,?,?,?)";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examineQuestionType.getExaminePaperId());
			pstm.setInt(2, examineQuestionType.getQuestionTypeId());
			pstm.setInt(3, examineQuestionType.getTypeScore());
			pstm.setInt(4, examineQuestionType.getQuestionTypeSum());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 删除卷题型
	public boolean deleteExamineQuestionType(int examineQuestionType_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "delete from e_q_type where e_q_type_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examineQuestionType_id);
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 更该卷题型
	public boolean updateExamineQuestionType(
			ExamineQuestionType examineQuestionType) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "update e_q_type set examination_paper_id=?,question_type_id=?,type_score=?,question_type_sum=? where e_q_type_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examineQuestionType.getExaminePaperId());
			pstm.setInt(2, examineQuestionType.getQuestionTypeId());
			pstm.setInt(3, examineQuestionType.getTypeScore());
			pstm.setInt(4, examineQuestionType.getQuestionTypeSum());
			pstm.setInt(5, examineQuestionType.getExamineQuestionTypeId());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 通过卷题型id，查询卷题型对象
	public ExamineQuestionType getExamineQuestionTypeByExamineQuestionTypeId(
			int examineQuestionType_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		ExamineQuestionType examineQuestiontype = null;
		PreparedStatement pstm = null;
		try {
			String sql = "select * from e_q_type where e_q_type_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examineQuestionType_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examineQuestiontype.setExamineQuestionTypeId(resultSet
						.getInt(1));
				examineQuestiontype.setExaminePaperId(resultSet.getInt(2));
				examineQuestiontype.setQuestionTypeId(resultSet.getInt(3));
				examineQuestiontype.setTypeScore(resultSet.getInt(4));
				examineQuestiontype.setQuestionTypeSum(resultSet.getInt(5));
				return examineQuestiontype;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 通过试卷id，查询该试卷中所有的卷题型id（内包含题型信息及其它关于题目的信息）
	public List<Integer> getExamineQuestionSubjectIdByExaminePaperId(
			int examinePaper_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> exaimneTypesId = new ArrayList<Integer>();
		try {
			String sql = "select e_q_type_id from e_q_type where examination_paper_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examinePaper_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				exaimneTypesId.add(resultSet.getInt(1));
			}
			return exaimneTypesId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// // 通过试题类型id，查询该题型被出过的所有试卷的卷题目id（内包含试卷信息及其它关于题目的信息）
	// // 通过题型找所有包括此题型的所有试卷
	// public List<Integer> getExamineQuestionSubjectIdByQuestionTypeId(
	// int examinePaper_id) {
	// return null;
	// }

	// 获得所有卷题型id
	public List<Integer> getAllExamineQuestionTypesId() {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> examineQuestionTypesId = new ArrayList<Integer>();
		try {
			String sql = "select e_q_type_id from e_q_type";
			pstm = connection.prepareStatement(sql);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examineQuestionTypesId.add(resultSet.getInt(1));
			}
			return examineQuestionTypesId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}
}
